/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.core.client;

public class Color {
	private int red, green, blue;
	
	public Color(int red, int green, int blue) {
		set(red,green,blue);
	}
	
	//- - - factory
	
	public static Color getRandomColor() {
		return new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
	}
	
	public static Color getRandomColor(int min, int max) {
	    max = max-min;
        return new Color((int)(Math.random()*max)+min,(int)(Math.random()*max)+min,(int)(Math.random()*max)+min);
    }
	
	//- - -
	
	public final void set(int red, int green, int blue) {
		this.red = red;
		this.green = green;
		this.blue = blue;
	}

    public final int getRed () {
        return red;
    }

    public final int getGreen () {
        return green;
    }

    public final int getBlue () {
        return blue;
    }

    public final String toHex() {
    	return toHex(red, green, blue);
    }
    
    public final String toRGB() {
        return toRGB(red,green,blue);
    }
    
	public final native static Color parse(String string) /*-{
	    if (string.charAt(0) == '#') string = string.substr(1);
        string = string.replace(/ /g,'');
        string = string.toLowerCase();
    
        var r = 0;
        var g = 0;
        var b = 0;
    
        var re = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
        
        var bits = re.exec(string);
        if (bits) { r = parseInt(bits[1]); g = parseInt(bits[2]); b = parseInt(bits[3]); }
        
        re = /^(\w{2})(\w{2})(\w{2})$/;
        
        var bits = re.exec(string);
        if (bits) { r = parseInt(bits[1], 16); g = parseInt(bits[2], 16); b = parseInt(bits[3], 16); }
        
        re = /^(\w{1})(\w{1})(\w{1})$/;
        
        var bits = re.exec(string);
        if (bits) { r = parseInt(bits[1]+bits[1], 16); g = parseInt(bits[2]+bits[2], 16); b = parseInt(bits[3]+bits[3], 16); }
        
        r = (r < 0 || isNaN(r)) ? 0 : ((r > 255) ? 255 : r);
        g = (g < 0 || isNaN(g)) ? 0 : ((g > 255) ? 255 : g);
        b = (b < 0 || isNaN(b)) ? 0 : ((b > 255) ? 255 : b);
        
        return @me.l1k3.core.client.Color::new(III)(r, g, b);
	}-*/;
	
	public final static String toHex(int red, int green, int blue) {
        return "#"+shortToHex(red)+shortToHex(green)+shortToHex(blue);
    }
	
	public final static String toRGB(int red, int green, int blue) {
        return "rgb(" + red + ", " + green + ", " + blue + ")";
    }

    private static native String shortToHex(int a) /*-{
        if (a==null) 
        return "00";
        
        a = parseInt(a); 
        
        if (a==0 || isNaN(a)) 
        return "00";
        
        a = Math.max(0,a); 
        a = Math.min(a,255); 
        a = Math.round(a);
        
        a = a.toString(16);
        if (a.length == 1) return "0" + a;
        
        return a;
    }-*/;
	
	public String toString() {
	    return toRGB();
	}
}
